from abc import ABC
import os

from Server.Handlers.basic import FileServerRequestHandler, Param
from Server.Utils import htmlUtils
from Server import const


class IndexHandle(FileServerRequestHandler, ABC):
    def get(self):
        routeFrom, route = self.getRfAndRoute()
        filePath = Param.RoutePath[route]

        if filePath == const.SpecialPath:
            self.write(htmlUtils.makeRootHtml(self, Param.RoutePath, Param.ProxyUrl))

        elif not os.path.exists(filePath):
            self.handlePathNotExists(filePath)

        elif self.needHide(filePath):  # 隐藏内容
            self.handleHiddenPath(filePath)

        else:
            fullUrl = self.request.full_url()
            if not fullUrl.endswith('/'):
                self.redirect(fullUrl + '/')
                return
            self.handleMakeDirHtml(filePath, route, routeFrom)
